﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellPrimitive_h
#define CellPrimitive_h

#include "CellPrerequisites.h"
#include "CellSerializable.h"
#include "CellStuff.h"

namespace cell
{
/** \addtogroup Core
 *  @{
 */
/** \addtogroup General
 *  @{
 */

/** 原语描述类。
@author
	Jiangwei Xu
*/
class _CellExport Primitive : public Serializable, public GeneralAlloc
{
public:
	/** 构造函数。
	*/
	Primitive(void);

	/** 构造函数。
	*/
	Primitive(const char* ownerTag);

	/** 构造函数。
	*/
	Primitive(const String& ownerTag);

	/** 析构函数。
	*/
	virtual ~Primitive(void);

public:
	// 列表定义
	typedef std::vector<SubjectStuff> SubjectList;
	typedef std::vector<PredicateStuff> PredicateList;
	typedef std::vector<ObjectiveStuff> ObjectiveList;
	typedef std::vector<AttributiveStuff> AttributiveList;
	typedef std::vector<AdverbialStuff> AdverbialList;
	typedef std::vector<ComplementStuff> ComplementList;

	/** 返回原语所属端的标签。
	*/
	const String& getOwnerTag(void) const;

	/** 提交主语。
	*/
	void commit(const SubjectStuff& subject);

	/** 提交谓语。
	*/
	void commit(const PredicateStuff& predicate);

	/** 提交宾语。
	*/
	void commit(const ObjectiveStuff& objective);

	/** 提交定语。
	*/
	void commit(const AttributiveStuff& attributive);

	/** 提交状语。
	*/
	void commit(const AdverbialStuff& adverbial);

	/** 提交补语。
	*/
	void commit(const ComplementStuff& complement);

	/** 返回主语列表。
	*/
	const SubjectList* subjects(void) const;

	/** 返回谓语列表。
	*/
	const PredicateList* predicates(void) const;

	/** 返回宾语列表。
	*/
	const ObjectiveList* objectives(void) const;

	/** 返回定语列表。
	*/
	const AttributiveList* attributives(void) const;

	/** 返回状语列表。
	*/
	const AdverbialList* adverbials(void) const;

	/** 返回补语列表。
	*/
	const ComplementList* complements(void) const;

	/** 清空所有语素。
	*/
	void clearStuff(void);

	/** 复制所有语素。
	*/
	void copyStuff(Primitive& dest) const;

	/** 是否为空原语。
	*/
	bool isEmpty(void) const;

	/** 设置关联的方言。
	*/
	void capture(Dialect* dialect, bool managed = false);

	/**
	*/
	Dialect* getDialect(void) const;

	/**
	*/
	bool hasDialect(void) const;

	/**
	*/
	Cellet* getContactedCellet(void);

public:
	/** 将原语数据写入序列化流。
	*/
	void write(SerializerStream& out);

	/** 从序列化流读取原语数据。
	*/
	void read(SerializerStream& in);

private:
	/**
	*/
	void contactCellet(Cellet* cellet) const;

private:
	String mOwnerTag;

	Dialect* mDialect;
	bool mManaged;		// 是否托管 Dialect 内存

	SubjectList mSubjects;
	PredicateList mPredicates;
	ObjectiveList mObjectives;
	AttributiveList mAttributives;
	AdverbialList mAdverbials;
	ComplementList mComplements;

	mutable Cellet* mCellet;
	friend class TalkService;
};

/** @} */
/** @} */
} // end namespace cell

#endif // CellPrimitive_h
